home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / CHIPCD_02_2002.iso / Internet / Macromedia ColdFusion Server 5 / coldfusion-50-win-us.exe / data1.cab / Examples / CFDOCS / snippets / cfloop.cfm < prev    next >
Encoding:
Text File  |  2001-06-13  |  2.5 KB  |  96 lines

  1. <CFQUERY NAME="GetEmployees" DATASOURCE="cfsnippets">
  2. SELECT     Emp_ID, FirstName, LastName, EMail, Phone, Department
  3. FROM       Employees
  4. </CFQUERY>
  5.  
  6. <!--- This example demonstrates the different kind
  7. of CFLOOP actions available --->
  8. <HTML>
  9.  
  10. <HEAD>
  11.  
  12. <TITLE>
  13. CFLOOP Example
  14. </TITLE>
  15. </HEAD>
  16.  
  17. <BASEFONT FACE="Arial, Helvetica" SIZE=2>
  18. <BODY  bgcolor="#FFFFD5">
  19.  
  20. <H3>CFLOOP Example</H3>
  21.  
  22. <P><B>Index loops</B>
  23. <BR>A simple loop: we can use an index parameter to
  24. tell us how many times we have looped through the expression.
  25. <P>
  26. <CFLOOP INDEX="counter" FROM="1" TO="5" STEP=1>
  27.     <CFOUTPUT>We have cycled through the loop #counter# time<CFIF counter is not 1>s</CFIF><BR></CFOUTPUT>
  28. </CFLOOP>
  29.  
  30. <P><B>Conditional loops</B>
  31. <BR>By stating a condition (tempVar is not 15), we can loop through
  32. and continue adding 3 onto tempVar; when tempVar is 15, the loop exits.
  33. <P>
  34. <CFPARAM name="tempVar" default=3>
  35. <CFLOOP CONDITION="tempVar is not 15">
  36.  
  37. <CFSET tempVar = tempVar + 3>
  38. <CFOUTPUT>#TempVar#</CFOUTPUT>
  39. </CFLOOP>
  40.  
  41. <P>Because tempVar now equals <CFOUTPUT>#tempVar#</CFOUTPUT>, 
  42. the condition is fulfilled.
  43.  
  44. <P><B>Looping over a query</B>
  45. <BR>Looping over a query can help us to display different
  46. output when a certain condition is reached (e.g. if the 
  47. Department of the employee is "Engineering," display in red):
  48.  
  49. <P>
  50. <CFLOOP QUERY="getEmployees">
  51.     <CFOUTPUT>
  52.         #FirstName# #LastName#,
  53.         <CFIF #Department# is "Engineering">
  54.             <font color=ff0000>#Department#</FONT>
  55.         <CFELSE>#Department#
  56.         </CFIF>
  57.         <BR>
  58.     </CFOUTPUT>
  59. </CFLOOP>
  60.  
  61. <P><B>Looping over a list</B>
  62. <P>CF gives the ability to loop over a list, which we
  63. can create for this example by looping over a new query
  64. and creating a list of FirstName/LastName combinations.
  65. <P>While looping through our query, we can create the list 
  66. variable, which we will then feed back to CFLOOP.
  67.  
  68. <CFQUERY NAME="GetFirstLastName" DATASOURCE="cfsnippets">
  69. SELECT FirstName, LastName
  70. FROM Employees
  71. ORDER by LastName
  72. </CFQUERY>
  73.  
  74. <!--- set up a temporary variable --->
  75. <CFSET tempList = "">
  76.  
  77. <!--- populate the list by looping through the query --->
  78. <CFLOOP QUERY="GetFirstLastName">
  79. <CFSET tempList = tempList & "#FirstName# #LastName#,">
  80. </CFLOOP>
  81.  
  82. <P>The list is as follows:
  83. <BR><CFOUTPUT>#tempList#</CFOUTPUT>
  84.  
  85. <P>CFLOOP outputs the list:
  86. <BR>
  87. <!--- loop through the resulting list --->
  88. <CFLOOP INDEX="ListElement" LIST="#tempList#" DELIMITERS=",">
  89.     <CFOUTPUT>#ListElement#</CFOUTPUT><BR>
  90. </CFLOOP>
  91.  
  92.  
  93. </BODY>
  94.  
  95. </HTML>       
  96.